#!/usr/bin/python
# -*- coding: utf-8 -*-
import gtk
import gobject
import sys
import os
import socket
import time
import locale, gettext

locale.setlocale(locale.LC_ALL, '')
APP = 'RDM'
DIR = os.path.join('share', 'locale')
#DIR = os.path.join(os.getcwd(), DIR)
gettext.bindtextdomain(APP, DIR)
gettext.textdomain(APP)
gettext.install(APP, DIR, unicode=1)
_ = gettext.gettext

class Main(gobject.GObject):
    def __init__(self):
        self.programName = ""
        self.tray = gtk.StatusIcon()
        self.tray.connect("activate", self.leftClickMenu)
        self.tray.connect("popup-menu", self.rightClickMenu)
        self.tray.set_tooltip(_("REMMenu"))
        pixbuf = gtk.gdk.pixbuf_new_from_file('icon.png')
        self.tray.set_from_pixbuf(pixbuf)
        self.tray.set_visible(True)
    
        from Menu import Menu
        self.menu2 = Menu()
        self.menu = gtk.Menu()
        self.addImageMenuitem(self.menu, gtk.STOCK_ABOUT, _('About'), self.aboutInfo)
        self.addImageMenuitem(self.menu, gtk.STOCK_QUIT,  _('QUIT'),  self.end)
        self.show = False
        self.gdkRoot = gtk.gdk.screen_get_default()
        gobject.threads_init()
        gtk.main()
        
    def rightClickMenu(self, icon, button, activate_time):
        self.menu.popup(None, None, gtk.status_icon_position_menu, button,  activate_time, self.tray)
        self.menu.show_all()
        
    def leftClickMenu(self, widget):
        self.menu2.showHideMenu()

    def addImageMenuitem(self, menu, stock=gtk.STOCK_MISSING_IMAGE, label=None, callback=None, cb_data=None,*args):
        item = gtk.ImageMenuItem(stock)
        if label is not None:
            item.get_child().set_text(label)
        if callback:
            if cb_data:
                item.connect('activate', callback, cb_data,*args)
            else:
                item.connect('activate', callback)
        menu.append(item)
        item.show()
        return item
    
    def aboutInfo(self, event):
        license = "The MIT License (MIT)\n\n"+ \
        "Copyright © 2011-2014 zenixls2\n"+ \
        "Permission is hereby granted, free of charge, to any person obtaining a copy\n"+ \
        " of this software and associated documentation files (the 'Software'), to deal\n"+ \
        " in the Software without restriction, including without limitation the rights to\n"+ \
        " use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies \n"+ \
        "of the Software, and to permit persons to whom the Software is furnished to do so,\n"+ \
        " subject to the following conditions:\n\n"+ \
        "The above copyright notice and this permission notice shall be included in all copies\n"+ \
        " or substantial portions of the Software.\n\n"+ \
        "THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,\n"+ \
        " INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A \n"+ \
        "PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT \n"+ \
        "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION \n"+ \
        "OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE\n"+ \
        " OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
        
        logo = gtk.gdk.pixbuf_new_from_file('icon.png')
        
        aboutDialog = gtk.AboutDialog()
        aboutDialog.set_name('Remote Download Manager Client')
        aboutDialog.set_program_name('Remote Download Manager Client')
        aboutDialog.set_version('0.1')
        aboutDialog.set_copyright('Copyright © 2011-2014 zenixls2')
        aboutDialog.set_comments('The client of Android application : Remote Download Manager')
        aboutDialog.set_license(license)
        aboutDialog.set_authors(['zenixls2', 'qcl'])
        aboutDialog.set_logo(logo)
        aboutDialog.run()
        aboutDialog.destroy()
        
        return True
        
    def hide(self, widget):
        widget.hide()
        
    def dummyDestroy(self, event):
        return True
        
    def end(self, event):
        gtk.main_quit()
        return True
        
if __name__ == '__main__':
    Main()
